##### Replication Data for "Seeking the Public Good: Public Service Motivation and Political Ambition" 
##### Authors: Hans J.G. Hassell, Gary E. Hollibaugh, Jr., and Matthew R. Miles
##### Email: gary.hollibaugh@pitt.edu

#### This code replicates Figures 1, 2, and A-1. All other figures and tables can 
#### be replicated by running the "Replication Code.do" Stata file in Stata.

### Clear the workspace and load necessary packages
rm(list = ls())
library(foreign)
library(haven)
library(ggplot2)
library(reshape2)

### Load the data
natl <- read_dta("PB-NationalSample.dta")
feds <- read_dta("PB-BureaucratSample.dta")
law <- read_dta("PB-LawStudentSample.dta")

### Figure 1
combined.frame.ambtypes <- data.frame(Electoral = c(as.numeric(table(feds$Elecamb)/sum(!is.na(feds$Elecamb))),
                                                    as.numeric(table(law$electedofficeamb)/sum(!is.na(law$electedofficeamb))),
                                                    as.numeric(table(natl$elecamb)/sum(!is.na(natl$elecamb)))),
                                      State = c(as.numeric(table(feds$Statamb)/sum(!is.na(feds$Statamb))),
                                                as.numeric(table(law$statelocalagencyamb)/sum(!is.na(law$statelocalagencyamb))),
                                                as.numeric(table(natl$statamb)/sum(!is.na(natl$statamb)))),
                                      Federal = c(as.numeric(table(feds$Execamb)/sum(!is.na(feds$Execamb))),
                                                  as.numeric(table(law$federalagencyamb)/sum(!is.na(law$federalagencyamb))),
                                                  as.numeric(table(natl$fedamb)/sum(!is.na(natl$fedamb)))),
                                      Judicial = c(as.numeric(table(feds$Judamb)/sum(!is.na(feds$Judamb))),
                                                   as.numeric(table(law$judicialamb)/sum(!is.na(law$judicialamb))),
                                                   as.numeric(table(natl$judamb)/sum(!is.na(natl$judamb)))),
                                      Sample = c(rep("Bureaucrats", 3), rep("Law Students", 3), rep("National", 3)),
                                      Type = rep(c("No Interest", "Open to\nPossibility", "Actively Working\nTowards"), 3))

combined.frame.ambtypes <- melt(combined.frame.ambtypes)
combined.frame.ambtypes$Type <- factor(combined.frame.ambtypes$Type, 
                                       levels = c("No Interest", 
                                                  "Open to\nPossibility", 
                                                  "Actively Working\nTowards"))

combined.frame.ambtypes$variable <- factor(combined.frame.ambtypes$variable, 
                                           labels = c("Electoral Ambition", 
                                                      "State Bureaucracy\nAmbition", 
                                                      "Federal Bureaucracy\nAmbition", 
                                                      "Judicial Ambition"))

ggplot(combined.frame.ambtypes, aes(x = Type, y = value, color = Sample, shape = Sample)) + 
  geom_point(position = position_dodge(width = .5), size = 2) + 
  facet_wrap(~ variable) + 
  theme_bw() + 
  theme(axis.text.x=element_text(angle=45,hjust=1)) + 
  scale_color_grey(start = 0.2, end = 0.6) + xlab('Level of Ambition') + ylab('Proportion of Respondents\nIndicating Level of Ambition\n') + ylim(0,.85)

ggsave("Figure-1.png", height = 4, width = 6)
ggsave("Figure-1.pdf", height = 4, width = 6)


### Figure 2
combined.frame <- data.frame(APM = c(natl$SC1, feds$PSMPM, law$SC1),
                             CPI = c(natl$SC2, feds$PSMPI, law$SC2),
                             SS = c(natl$SC13, feds$PSMSS, law$SC4),
                             COM = c(natl$SC12, feds$PSMCOM, law$SC3),
                             Sample = c(rep("National", dim(natl)[1]), 
                                        rep("Bureaucrats", dim(feds)[1]), 
                                        rep("Law Students", dim(law)[1])))

combined.frame <- melt(combined.frame)
combined.frame$variable <- factor(combined.frame$variable, 
                                  labels = c("Attraction to\nPolicy Making",
                                             "Commitment to\nPublic Interest",
                                             "Self-Sacrifice",
                                             "Compassion"))

ggplot(combined.frame, aes(x = variable, y = (value - 3)/12)) + 
  geom_violin(aes(fill = Sample), alpha = I(0.3), width = 0.7) + 
  geom_boxplot(aes(fill = Sample), alpha = I(0.3), width = 0.1, 
               position = position_dodge(width = .7), 
               outlier.shape = NA) + 
  scale_fill_grey(start = 0, end = 1) + 
  xlab("\nPSM Component") + 
  ylab("PSM Component Score\n") +
  theme_bw() + theme(axis.text.x=element_text(angle=45,hjust=1))

ggsave("Figure-2.png", height = 4, width = 6)
ggsave("Figure-2.pdf", height = 4, width = 6)


### T-tests in footnote
t.test((natl$SC1 - 3)/12, (feds$PSMPM - 3)/12)
t.test((natl$SC2 - 3)/12, (feds$PSMPI - 3)/12)
t.test((natl$SC12 - 3)/12, (feds$PSMCOM - 3)/12)
t.test((natl$SC13 - 3)/12, (feds$PSMSS - 3)/12)

t.test((natl$SC1 - 3)/12, (law$SC1 - 3)/12)
t.test((natl$SC2 - 3)/12, (law$SC2 - 3)/12)
t.test((natl$SC12 - 3)/12, (law$SC3 - 3)/12)
t.test((natl$SC13 - 3)/12, (law$SC4 - 3)/12)

t.test((feds$PSMPM - 3)/12, (law$SC1 - 3)/12)
t.test((feds$PSMPI - 3)/12, (law$SC2 - 3)/12)
t.test((feds$PSMCOM - 3)/12, (law$SC3 - 3)/12)
t.test((feds$PSMSS - 3)/12, (law$SC4 - 3)/12)


### Figure A-1
combined.frame.reasons <- data.frame(Solutions = c(as.numeric(table(law$reasons_1)/sum(!is.na(law$reasons_1))),
                                                    as.numeric(table(natl$Q34_7_1)/sum(!is.na(natl$Q34_7_1)))),
                                      Meeting = c(as.numeric(table(law$reasons_2)/sum(!is.na(law$reasons_2))),
                                                as.numeric(table(natl$Q34_7_2)/sum(!is.na(natl$Q34_7_2)))),
                                      Fundraising = c(as.numeric(table(law$reasons_3)/sum(!is.na(law$reasons_3))),
                                                  as.numeric(table(natl$Q34_7_3)/sum(!is.na(natl$Q34_7_3)))),
                                      Publicity = c(as.numeric(table(law$reasons_4)/sum(!is.na(law$reasons_4))),
                                                   as.numeric(table(natl$Q34_7_4)/sum(!is.na(natl$Q34_7_4)))),
                                      Persuasion = c(as.numeric(table(law$reasons_5)/sum(!is.na(law$reasons_5))),
                                                   as.numeric(table(natl$Q34_7_5)/sum(!is.na(natl$Q34_7_5)))),
                                      Competition = c(as.numeric(table(law$reasons_6)/sum(!is.na(law$reasons_6))),
                                                   as.numeric(table(natl$Q34_7_6)/sum(!is.na(natl$Q34_7_6)))),
                                      Privacy = c(as.numeric(c(table(law$reasons_8), 0)/sum(!is.na(law$reasons_8))),   # none in last category
                                                   as.numeric(table(natl$Q34_8_1)/sum(!is.na(natl$Q34_8_1)))),
                                      Decisions = c(as.numeric(table(law$reasons_9)/sum(!is.na(law$reasons_9))),
                                                   as.numeric(table(natl$Q34_8_2)/sum(!is.na(natl$Q34_8_2)))),
                                      Scrutiny = c(as.numeric(table(law$reasons_10)/sum(!is.na(law$reasons_10))),
                                                   as.numeric(table(natl$Q34_8_3)/sum(!is.na(natl$Q34_8_3)))),
                                      Schedule = c(as.numeric(table(law$reasons_11)/sum(!is.na(law$reasons_11))),
                                                   as.numeric(table(natl$Q34_8_4)/sum(!is.na(natl$Q34_8_4)))),
                                      Family = c(as.numeric(table(law$reasons_12)/sum(!is.na(law$reasons_12))),
                                                   as.numeric(table(natl$Q34_8_5)/sum(!is.na(natl$Q34_8_5)))),
                                      Conflict = c(as.numeric(table(law$reasons_14)/sum(!is.na(law$reasons_14))),
                                                   as.numeric(table(natl$Q34_8_7)/sum(!is.na(natl$Q34_8_7)))),
                                      Sample = c(rep("Law Students", 3), rep("National", 3)),
                                      Type = rep(c("Less Likely", "Neither/Neutral", "More Likely"), 2))



combined.frame.reasons <- melt(combined.frame.reasons)
combined.frame.reasons$Type <- factor(combined.frame.reasons$Type, 
                                       levels = c("Less Likely", 
                                                  "Neither/Neutral", 
                                                  "More Likely"))

combined.frame.reasons$variable <- factor(combined.frame.reasons$variable, 
                                           labels = c("Talking About Solutions", 
                                                      "Meeting New People", 
                                                      "Fundraising", 
                                                      "Publicity",
                                                      "Voter Persuasion",
                                                      "Competition",
                                                      "Lack of Privacy",
                                                      "Making Decisions",
                                                      "Public Scrutiny",
                                                      "Schedule/Traveling",
                                                      "Difficult on Family\nand Friends",
                                                      "Conflict"))

ggplot(combined.frame.reasons, aes(x = Type, y = value, 
                                   color = Sample, shape = Sample)) + 
  geom_point(position = position_dodge(width = .5), size = 2) + 
  facet_wrap(~ variable) + 
  theme_bw() + 
  theme(axis.text.x=element_text(angle=45,hjust=1)) + 
  scale_color_grey(start = 0.2, end = 0.6) + 
  xlab('\nHow Aspect of Public Service Affects Interest in Running for Office') + 
  ylab('Proportion of Respondents Indicating Response\n') + ylim(0,1)

ggsave("Figure-A1.png", height = 6, width = 9)
ggsave("Figure-A1.pdf", height = 6, width = 9)
